import $ from '@/assets/js/jquery-vendor.js'
import layer from '#/layer/layer.min.js'
import plupload from 'plupload'

window.upload_multi_file = function (btn, divId, _fileExt, name, size, callback) {
  var fileMaxSize = 50;
  var fileExtensions = _fileExt || 'doc';
  var uploader = new plupload.Uploader({
    runtimes: 'html5,flash,silverlight,html4',
    browse_button: '' + btn,
    multi_selection: true,
    container: document.getElementById('' + divId),
    url: window.uploadPath + '/file/fileUpload?dir=file',

    filters: {
      mime_types: [
        {title: 'all files', extensions: fileExtensions}
      ],
      max_file_size: fileMaxSize + 'mb', // 最大只能上传mb的文件
      prevent_duplicates: false // 不允许选取重复文件
    },

    init: {
      PostInit: function () {
      },
      FilesAdded: function (up, files) {
        var curSize = $(`#${divId} .file-grid-3`).size();
        if (curSize >= size) {
          alert('最多只能上传' + size + '个文件!');
          return false;
        }
        plupload.each(files, function (file) {
          $(`#${divId}`).append(`
            <div id="${file.id}" class="file-grid-1 file-grid-2">
              <b></b>
              <div class="progress">
                <div class="progress-bar" style="width: 0%">
                </div>
              </div>
            </div>`);
          uploader.start();
        });
      },
      BeforeUpload: function (up, file) {
        $(`#${btn}`).css('pointer-events', 'none');
      },
      // 上传进度
      UploadProgress: function (up, file) {
        $(`#${file.id} b`).html(`<span>${file.percent}%</span>`);
        $(`#${file.id} .progress-bar`).width(2 * file.percent);
        $(`#${file.id} .progress-bar`).attr('aria-valuenow', 2 * file.percent);
      },
      // 文件上传完毕
      FileUploaded: function (up, file, info) {
        $(`#${divId} .file-grid-1`).remove();
        if (info.status === 200) {
          var result = JSON.parse(info.response);
          if (result.code !== 0) {
            $(`#${divId}`).append(result.message);
          } else {
            var html = `
              <div id="${file.id}" class="file-grid-2 file-grid-3 inline">
                <input name="${name}" value="${result.data.url}" type="hidden">
                <input name="factFileName" value="${file.name}" type="hidden">
                <a href="${result.data.url}" target="_blank">${file.name}</a>
                <div><a href="javascript:;" onclick="removeFile(this);">删除</a></div>
              </div>`;
            $(`#${divId}`).append(html);
            // window.$vueApp.item[name] = result.data.url;
            callback && callback(result.data.url);
          }
        } else {
          $(`#${divId}`).append(info.response);
        }
        $(`#${btn}`).css('pointer-events', '');
      },

      Error: function (up, err) {
        if (err.code === -600) {
          layer.msg('选择的文件太大了,不能超过' + fileMaxSize + 'M');
        } else if (err.code === -601) {
          layer.msg('只能上传' + fileExtensions + '格式大小的' + fileMaxSize);
        } else if (err.code === -602) {
          layer.msg('这个文件已经上传过一遍了');
        } else {
          layer.msg('上传异常');
        }
        $(`#${divId}` + ' .file-grid-1').remove();
      }
    }
  });
  uploader.init();
  setTimeout(function () { $('.moxie-shim').css('top', 0); }, 500);
  return uploader;
}
window.removeFile = function (obj) {
  $(obj).parent().parent().remove();
}
